from __future__ import with_statement
import bootstrap
bootstrap.bootstrap(__file__)

from pyceab.course.models import Course
import os
import re
import simplejson as json
import sys

COURSE_CODE_RE = re.compile(r'([^0-9]+)(\d*[A-Z]?)')

def load_te(te_data_path):
    with open(te_data_path, 'r') as f_in:
        tes = json.load(f_in)

    for e in tes:
        c = Course()
        department, course_num = COURSE_CODE_RE.match(e['course_code']).groups()
        c.department = department
        c.course_code = course_num
        c.full_name = e['course_name']
        c.te_math = e['math']
        c.te_sci = e['sci']
        c.te_engsci = e['engsci']
        c.te_engdes = e['engdes']
        print c.short_form
        c.save()

def load_cse(cse_data_path):
    cse_categories = ['a', 'b', 'c', 'd']

    with open(cse_data_path, 'r') as f_in:
        cses = json.load(f_in)

    for category in cse_categories:
        for e in cses[category]:
            c = Course()
            department, course_num = COURSE_CODE_RE.match(e['course_code']).groups()
            c.department = department
            c.course_code = course_num
            c.full_name = e['course_name']
            c.__setattr__('cse_' + category, e['weight'])
            c.save()

def main():
    load_te(sys.argv[1])
    load_cse(sys.argv[2])

if __name__ == '__main__':
    main()
